#!/usr/bin/perl -w
########################################################################
# script name:  info_users.pl
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2011/02/25    0.1     MDUMONT         initial
########################################################################
use strict;
my $type="user";

my $hostname = `/bin/uname -n`; chomp $hostname;
my $hostid = `/usr/sbin/hostid`; chomp $hostid;
my $dt = `/bin/date +"%Y-%m-%d %H:%M:%S"`; chomp $dt;

my $outfile = "/var/OPM/data/${hostname}_${type}.out";
open (OUT, ">$outfile");
print OUT '<uname -n>',"\n",$hostname,"\n",'</uname -n>', "\n";
print OUT '<hostid>',"\n",$hostid,"\n",'</hostid>', "\n";
print OUT '<date>',"\n",$dt,"\n",'</date>', "\n";

my(%UIDS, %GIDS, %GROUP,%SHADOW, %USERS, %SUDO);

my(%PROJECT,%CRON,$cron);

if (-e "/etc/cron.d/cron.allow")
{ 
	$cron = "RESTRICTED"; 
	my @TMP = `/bin/cat /etc/cron.d/cron.allow`;
	foreach my $tmp (@TMP){chomp $tmp;$CRON{$tmp}++;}
} 
else {$cron = "N/A";}

# Processing "sudoers"
{
	my %ALIASES;
	my (@TMP);
	if (-e "/etc/sudoers"){@TMP=`cat /etc/sudoers`;}
	if (-e "/usr/local/etc/sudoers"){@TMP=`cat /usr/local/etc/sudoers`;}
	foreach my $tmp (@TMP)
	{
		$tmp =~ s/\#.*//g; $tmp =~ s/\s+/ /g; $tmp =~ s/^ //g; $tmp =~ s/ $//g;
		if ($tmp eq ""){}
		elsif ($tmp =~ /^Cmnd_Alias [A-Z0-9_-]+ *= */){}
		elsif ($tmp =~ /^Host_Alias [A-Z0-9_-]+ *= */){}
		elsif ($tmp =~ /^Runas_Alias [A-Z0-9_-]+ *= */){}
		elsif ($tmp =~ /^User_Alias ([A-Z0-9_-]+) *= *(.+)/){
			my ($alias, $users) = ($1,$2);
			my @TMP2=split /\s*,\s*/, $users;
			$ALIASES{$alias}=\@TMP2;
			
		}
		elsif ($tmp =~ /^([\w\d_-]+) ([\w\d_-]+) *= *(.+)/)
		{
			my ($u,$w,$c) = ($1,$2,$3);
			if (exists $ALIASES{$u})
			{
				foreach my $u2 (@{$ALIASES{$u}})
				{
					if (exists ( $SUDO{$u2})) { push @{$SUDO{$u2}}, $c; } 
					else { $SUDO{$u2} = [$c]; } 
				}
			}
			else {
				if (exists ( $SUDO{$u})){ push @{$SUDO{$u}}, $c;} 
				else {$SUDO{$u} = [$c];} 
			}
			
		}
		else {
		}
	
	}
}
	
# Processing "group"
{
	print OUT '<group>',"\n";
	my (@TMP) = `cat /etc/group`;;
	foreach my $tmp (@TMP){
		my ($grp,$x,$gid,$users) = split /:/, $tmp;
		$users =~ s/\s+//;
		$GIDS{$gid}=$grp;
		print OUT "$grp\t$gid\t$users\n";
		my @TMP2 = split /,/, $users;
		foreach my $user (@TMP2){
			if (exists ( $GROUP{$user})){ push @{$GROUP{$user}}, $grp;} 
			else {$GROUP{$user} = [$grp];} 
		}
	}
	print OUT '</group>',"\n";
}

# Processing "passwd"
{
	print OUT '<users>',"\n";
	my (@TMP) = `/usr/sbin/lsuser -a id groups pgrp gecos shell home ALL`;
	my $_tmp = join("",@TMP);
	my @a_split = split /\n/, $_tmp;
	foreach my $xid (0..$#a_split) {
       		my ($user,$uid,$groups,$grp,$full,$shell,$home,$pass,$chg) = ('-','-','-','-','-','-','-','-','-');
       		my ($daysmin,$daysmax,$dayswarn,$daysinact,$expire,$cron_allowed,$flag) = ('-','-','-','-','-','-','-');
	       	if ( $a_split[$xid] =~ /^(.*) id/ ){ $user = $1; }
	        if ( $a_split[$xid] =~ /id=(\d+) groups/ ){ $uid = $1; }
	        if ( $a_split[$xid] =~ /groups=(.*) pgrp/ ){ $groups = $1; }
	        if ( $a_split[$xid] =~ /pgrp=([aA-zZ]+) / ){ $grp = $1; }
	        if ( $a_split[$xid] =~ /gecos=(.*) shell/ ){ $full = $1; }
	        if ( $a_split[$xid] =~ /shell=(.+) / ){ $shell = $1; }
	        if ( $a_split[$xid] =~ /home=(.+)/ ){ $home = $1; }
		my $gid = `/usr/sbin/lsgroup -a id $grp | cut -d= -f2`; chomp $gid;
		#my (@_gid) = `/usr/sbin/lsgroup -a id $grp`;
		#my $s_gid = join(" ",@TMP);
		#my $gid = ($s_gid =~ /id=(\d+)$/)?$1:"-";
		my $tmp2 = `/usr/bin/last -n 1 $user|/bin/grep $user`;
		$tmp2 =~ s/\t/ /g;
		my @TMP2 = split /\s+/,$tmp2;
		shift @TMP2;shift @TMP2;shift @TMP2; pop @TMP2;
		my $lastlogin = ($tmp2=~/^$user/)?join(" ",@TMP2):"-";
		my @_passwd = `cat /etc/security/passwd`;
		print OUT "$uid\t$user\t$gid\t$grp\t$groups\t$full\t$home\t$shell\t";
		print OUT "$pass\t$chg\t$daysmin\t$daysmax\t$dayswarn\t$daysinact\t$expire\t$cron_allowed\t$flag\n";
	}
	print OUT '</users>',"\n";
}
	
sub HomeSize {
	my $home = shift;
	my $size = -1;
	if (-e $home && $home ne "/"){
		my $du = `/usr/bin/du -ks $home`;
		chomp $du;
		if ($du =~ /.+\s(\d+)\s*$/){$size = $1;}
	}
	return $size;
}

close OUT;
`chown saunix $outfile`;
	
